package cn.kxm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.pojo.Company;
import cn.kxm.pojo.InquiryBill;
import cn.kxm.pojo.InquiryBillDetaill;
import cn.kxm.pojo.OutstorageBill;
import cn.kxm.service.CompanyService2;
import cn.kxm.service.InquiryBillService;
import cn.kxm.service.PartService2;

@Controller
@RequestMapping("/IBC")
public class InquirybillControler extends BaseController{
	@Autowired
	private InquiryBillService ibs;
	@Autowired
	private CompanyService2  com;;
	
	
	@RequestMapping("show")
	@ResponseBody
	public Object  findXunjiaDan(
			@RequestParam(value="findcode",required=false)String code,
			@RequestParam(value="findstartDate",required=false)String start,
			@RequestParam(value="findendDate",required=false)String end,
			@RequestParam(value="findsupplierName",required=false)String name,
			Integer page,Integer rows			
			){
		int total=ibs.getCount(code,start,end,name);
		System.out.println(total);
		List<InquiryBill> list=new ArrayList<InquiryBill>();
		list=ibs.findAllInquirybill(code,start,end,name,(page-1)*rows,rows);
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("rows", list);
		map.put("total", total);
		  System.out.println(JSONArray.toJSONString(map));
		return JSONArray.toJSONString(map);
		
	}
	
	
	
	@RequestMapping("PartBycode")
	@ResponseBody
	public Object  findpartAllBycode(@RequestParam(value="code",required=false)String code){
    List<Map<String, String>>	 list=   ibs.findDetailByBillCode(code);
    Map<String, Object> map=new HashMap<String, Object>();
          map.put("rows", list);
          for (Map<String, String> map1 : list) {
			 System.out.println(map1.get("memo")+map1.get("partName"));
		}
		  return JSONArray.toJSONString(map);
	}
  //保存
	@RequestMapping("baocun")
	@ResponseBody	
	public  Object   addPart(InquiryBill i,String data,String mm,Double money){
		i.setMoney(money);
	
		Map<String, Object> map=new HashMap<String, Object>();	
		
		if(ibs.addInquiry(i)!=1){
			map.put("result", "error");
			return JSON.toJSONString(map);
		};
		
      List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
      if(list!=null){
    	    for (int j = 0; j <list.size()-1; j++) {
    	    	Map<String, Object> m=list.get(j);
    	    	InquiryBillDetaill  ibd=new InquiryBillDetaill();
    	    	ibd.setAmount(Integer.parseInt(m.get("amount").toString()));
    	    	ibd.setPartCode(m.get("code").toString());
    	    	ibd.setPrice((Integer)m.get("price"));
    	    	ibd.setBillCode(i.getCode());
    	    	ibd.setMemo(i.getMemo());   	    	
    	    	if(ibs.addInquiryDtail(ibd)!=1){
    	    		  System.out.println("jin list!=null");
					map.put("result", "error");
					return JSON.toJSONString(map);
				}  	         
			}
    	  
    	  
      }else{
    	  System.out.println("jin lisTNull");
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
        map.put("result", "success");       
		return  map;		
	}
//修改 
	@RequestMapping("modifyone")
	public String modifyone(String code,Model model){
		//OutstorageBill oBill=os.findByCode(code);
	     InquiryBill  	iqb=    ibs.findByCode(code);	     
         String  name=  iqb.getSupplierName();	  
         System.out.println(iqb.getInquiryTime());
         Company     coy=   com.getCompanyFax(name);       
		 model.addAttribute("oB", iqb);
		 model.addAttribute("coy", coy);
		 System.out.println();	
		 return "web/goods/modifyIquiryBill";
	}
//modiftyIquiry
	@RequestMapping("modiftyIquiry")
	@ResponseBody	
	public  Object   modiftyIquiry(InquiryBill i,String data,String mm,Double money ){
		System.out.println("hgf");
		System.out.println(i.toString());
		i.setMoney(money);
		
		
	System.out.println(data+"---"+i.toString());
		Map<String, Object> map=new HashMap<String, Object>();		
		if(ibs.modiftyIquiryBill(i)!=1){
			map.put("result", "error");
			return JSON.toJSONString(map);
		};
		
      List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
      List<String> memos=(List<String>)JSON.parse(mm);
      if(list!=null){
    		ibs.deletinquirybilldetaill(i.getCode());	
    	    for (int j = 0; j <list.size()-1; j++) {
    	    	Map<String, Object> m=list.get(j);
    	    	String ms=memos.get(j);
    	    	InquiryBillDetaill  ibd=new InquiryBillDetaill();    	    	
    	    	ibd.setAmount(Integer.parseInt(m.get("amount").toString()));    	    	    	    	
    	    	ibd.setPartCode(m.get("code").toString());
    	    	ibd.setPrice((Integer)m.get("price"));
    	    	ibd.setBillCode(i.getCode());
    	    	ibd.setMemo(ms);   	    	
    	    	if(ibs.addInquiryDtail(ibd)!=1){
    	    		  System.out.println("jin list!=null");
					map.put("result", "error");
					return JSON.toJSONString(map);
				}  	         
			}    	  
    	  
      }else{
    	  System.out.println("jin lisTNull");
			map.put("result", "error");
			return JSON.toJSONString(map);
		}
        map.put("result", "success");       
		return  map;		
		
	}
//单行删除
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String code){
		System.out.println(code+"jin");
		Map<String, Object> map=new HashMap<String, Object>();
	          int result=	   ibs.deletinquirybilldetaill(code);
	  
		    if(result>=0){
			   ibs.deletinquirybill(code);	 
			   map.put("result", "error");
		    }        
	     	
		/*if(ibs.deletinquirybilldetaill(code)>0&&ibs.deletinquirybill(code)>0){//先删除涉及外键的详单
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}*/
		
		return JSON.toJSONString(map);
	}
	
//批量删除	
	@RequestMapping("del")
	@ResponseBody
	public Object del(String data){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		for (Map<String, Object> map2 : list) {
     int	  result  =		ibs.deletinquirybilldetaill(map2.get("code").toString());
			  if(result>=0){
				 int   aaa= ibs.deletinquirybill(map2.get("code").toString());
					 if(aaa>0){
						 
						 map.put("result", "success"); 
					 }else{
							map.put("result", "error");
					 }
				
					  
			  }
			  
			/*if(ibs.deletinquirybilldetaill(map2.get("code").toString())>0&&ibs.deletinquirybill(map2.get("code").toString())>0){//先删除涉及外键的详单
				//System.out.println("进----"+"批量");
				map.put("result", "success");
			}else{
				map.put("result", "error");
			}*/
		}
		
		return JSON.toJSONString(map);
	}
//
	@RequestMapping("shenhela")
	@ResponseBody
	public  Object shenhe(String code){
		String result= ibs.shenhe(code);		
		      if(result.equals("success")){
		    	  return "success";		    	  		    	  
		      }		
		         return "error";	
	  }
	//chexiao
	@RequestMapping("chexiaola")
	@ResponseBody
	public  Object chexiao(String code){
		String result= ibs.chexiaoService(code);		
		      if(result.equals("success")){
		    	  return "success";		    	  		    	  
		      }		
		         return "error";	
	  }	
  //isdel	
	@RequestMapping("isdel")
	@ResponseBody
	public  Object isdel(String code){
		System.out.println(code);
		System.out.println("进入isdel");
		String result= ibs.isdelService(code);		
		       if(result.equals("已审核")){
		    	  return "2";		    	  		    	  
		       }		
		         return "1";	
	  }	
	
	
	
}
